package com.ruoyi.rental.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.landlord.mapper.HumanLandlordMapper;
import com.ruoyi.room.mapper.HumanRoomMapper;
import com.ruoyi.user_list.mapper.MobileUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.rental.mapper.RentalAgreementsByNameMapper;
import com.ruoyi.rental.domain.RentalAgreementsByName;
import com.ruoyi.rental.service.IRentalAgreementsByNameService;

/**
 * 租约管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-15
 */
@Service
public class RentalAgreementsByNameServiceImpl implements IRentalAgreementsByNameService
{
    @Autowired
    private RentalAgreementsByNameMapper rentalAgreementsByNameMapper;

    @Autowired
    private MobileUserMapper mobileUserMapper;

    @Autowired
    private HumanRoomMapper humanRoomMapper;

    @Autowired
    private HumanLandlordMapper humanLandlordMapper;

    /**
     * 查询租约管理
     *
     * @param rentalId 租约管理主键
     * @return 租约管理
     */
    @Override
    public RentalAgreementsByName selectRentalAgreementsByNameByRentalId(Long rentalId)
    {
        return rentalAgreementsByNameMapper.selectRentalAgreementsByNameByRentalId(rentalId);
    }

    /**
     * 查询租约管理列表
     *
     * @param rentalAgreementsByName 租约管理
     * @return 租约管理
     */
    @Override
    public List<RentalAgreementsByName> selectRentalAgreementsByNameList(RentalAgreementsByName rentalAgreementsByName)
    {
        return rentalAgreementsByNameMapper.selectRentalAgreementsByNameList(rentalAgreementsByName);
    }

    /**
     * 新增租约管理
     *
     * @param rentalAgreementsByName 租约管理
     * @return 结果
     */
    @Override
    public int insertRentalAgreementsByName(RentalAgreementsByName rentalAgreementsByName)
    {
        rentalAgreementsByName.setUserName(mobileUserMapper.selectMobileUserNameByUserId(rentalAgreementsByName.getUserName()));
        rentalAgreementsByName.setLandlordName(humanLandlordMapper.selectHumanLandlordByLandlordName(rentalAgreementsByName.getLandlordName()));
        rentalAgreementsByName.setRoomNumber(humanRoomMapper.selectHumanRoomNameByRoomId(rentalAgreementsByName.getRoomNumber()));
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, Math.toIntExact(rentalAgreementsByName.getLeaseTerm()));
        rentalAgreementsByName.setExpirationDate(calendar.getTime());
        return rentalAgreementsByNameMapper.insertRentalAgreementsByName(rentalAgreementsByName);
    }

    /**
     * 修改租约管理
     *
     * @param rentalAgreementsByName 租约管理
     * @return 结果
     */
    @Override
    public int updateRentalAgreementsByName(RentalAgreementsByName rentalAgreementsByName)
    {
        if (rentalAgreementsByName.getUserName() != null){
            rentalAgreementsByName.setUserName(mobileUserMapper.selectMobileUserNameByUserId(rentalAgreementsByName.getUserName()));
        }
        if (rentalAgreementsByName.getLandlordName() != null){
            rentalAgreementsByName.setLandlordName(humanLandlordMapper.selectHumanLandlordByLandlordName(rentalAgreementsByName.getLandlordName()));
        }
        if (rentalAgreementsByName.getRoomNumber()!=null){
            rentalAgreementsByName.setRoomNumber(humanRoomMapper.selectHumanRoomNameByRoomId(rentalAgreementsByName.getRoomNumber()));
        }
        return rentalAgreementsByNameMapper.updateRentalAgreementsByName(rentalAgreementsByName);
    }

    /**
     * 批量删除租约管理
     *
     * @param rentalIds 需要删除的租约管理主键
     * @return 结果
     */
    @Override
    public int deleteRentalAgreementsByNameByRentalIds(Long[] rentalIds)
    {
        return rentalAgreementsByNameMapper.deleteRentalAgreementsByNameByRentalIds(rentalIds);
    }

    /**
     * 删除租约管理信息
     *
     * @param rentalId 租约管理主键
     * @return 结果
     */
    @Override
    public int deleteRentalAgreementsByNameByRentalId(Long rentalId)
    {
        return rentalAgreementsByNameMapper.deleteRentalAgreementsByNameByRentalId(rentalId);
    }

    //新建方法，用于检测是否过期，若过期则删除,设置定时为每个月第一天执行
    @Override
    //使用Scheduled注解，实现每个月第一天执行

    @Scheduled(cron = "0 0 0 1 * ?")
    public void checkExpiration() {
        List<RentalAgreementsByName> list = rentalAgreementsByNameMapper.selectRentalAgreementsByNameList(new RentalAgreementsByName());
        Calendar calendar = Calendar.getInstance();
        Date date=calendar.getTime();
        for (RentalAgreementsByName rentalAgreementsByName : list) {
            if (date.after(rentalAgreementsByName.getExpirationDate())){
                rentalAgreementsByNameMapper.deleteRentalAgreementsByNameByRentalId(rentalAgreementsByName.getRentalId());
            }
        }
    }
}
